package com.atguigu.tingshu.album.service.impl;

import java.math.BigDecimal;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.Date;

import com.google.common.collect.Maps;

import com.atguigu.tingshu.album.mapper.BaseCategory1Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategory2Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategory3Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategoryViewMapper;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.vo.album.AlbumInfoVo;
import com.atguigu.tingshu.vo.album.CategoryVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;


    @Override
    public List<CategoryVo> getBaseCategoryList() {
        // 查询视图
        return baseCategoryViewMapper.getBaseCategoryList();
    }

    @Override
    public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
        // 1. 根据category1Id 查询category2
        LambdaQueryWrapper<BaseCategory2> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaseCategory2::getCategory1Id, category1Id).eq(BaseCategory2::getIsDeleted, 0).select(BaseCategory2::getId);
        List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(baseCategory2List))
            throw new GuiguException(201, "该一级分类下没有二级分类");
        List<Long> baseCategory2IdList = baseCategory2List.stream().map(BaseCategory2::getId).toList();
        // 2. 根据category2Id 查询category3 需要7个
        LambdaQueryWrapper<BaseCategory3> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.in(BaseCategory3::getCategory2Id, baseCategory2IdList).eq(BaseCategory3::getIsDeleted, 0).eq(BaseCategory3::getIsTop, 1).last("limit 7");
        List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(wrapper2);
        return baseCategory3List;
    }

    @Override
    public BaseCategoryView getCategory(Long albumId) {
        return baseCategoryViewMapper.getAlbumCategory(albumId);
    }

    @Override
    public CategoryVo getBaseCategoryResult(Long category1Id) {
        // 1.查询一级分类信息
        BaseCategory1 baseCategory1 = baseCategory1Mapper.selectById(category1Id);
        // 2.封装一级分类信息
        if (baseCategory1 == null)
            throw new GuiguException(201, "该一级分类不存在");
        CategoryVo categoryVo1 = new CategoryVo();
        categoryVo1.setCategoryId(baseCategory1.getId());
        categoryVo1.setCategoryName(baseCategory1.getName());
        ArrayList<CategoryVo> categoryVoArrayList2 = new ArrayList<>();
        // 3.查询二级分类信息
        List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, category1Id).eq(BaseCategory2::getIsDeleted, 0));
        if (CollectionUtils.isEmpty(baseCategory2List))
            throw new GuiguException(201, "该一级分类下没有二级分类");
        for (BaseCategory2 baseCategory2 : baseCategory2List) {
            CategoryVo categoryVo2 = new CategoryVo();
            categoryVo2.setCategoryId(baseCategory2.getId());
            categoryVo2.setCategoryName(baseCategory2.getName());
            categoryVoArrayList2.add(categoryVo2);
            ArrayList<CategoryVo> categoryVoArrayList3 = new ArrayList<>();
            List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>().eq(BaseCategory3::getCategory2Id, baseCategory2.getId()).eq(BaseCategory3::getIsDeleted, 0));
            if(CollectionUtils.isEmpty(baseCategory3List))
                throw new GuiguException(201, "该二级分类下没有三级分类");
            for (BaseCategory3 baseCategory3 : baseCategory3List) {
                CategoryVo categoryVo3 = new CategoryVo();
                categoryVo3.setCategoryId(baseCategory3.getId());
                categoryVo3.setCategoryName(baseCategory3.getName());
                categoryVo3.setCategoryChild(new ArrayList<>());
                categoryVoArrayList3.add(categoryVo3);
            }
            categoryVo2.setCategoryChild(categoryVoArrayList3);
        }
        categoryVo1.setCategoryChild(categoryVoArrayList2);
        return categoryVo1;
    }

    @Override
    public List<Long> getC1Ids() {
        return baseCategory1Mapper.selectList(null).stream().map(BaseCategory1::getId).toList();
    }
}

